/**
 * @file       CPagingByObjectStrategy.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Jan 31, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include "PagingByObjectStrategy.h"
#include "errorcode.h"
#include "PageInfo.h"

namespace NS_LCD_AFW
{

CPagingByObjectStrategy::CPagingByObjectStrategy()
{
}

CPagingByObjectStrategy::~CPagingByObjectStrategy()
{
}

int CPagingByObjectStrategy::calculateViewPort(
		const CFramePtrArray & framePtrArray, const CRect & parentRect,
		CPageInfo & pageResult)
{
	int curPage = pageResult.getCurPage();
	int curHightlightObjectIndex = pageResult.getHightLightObjIndex();
	int nextHightlightObjectIndex = -1;

	switch (pageResult.getDirection())
	{
	case CPageInfo::UP:
	{
		nextHightlightObjectIndex = findNextHightlightableObjectIndex(
				framePtrArray, curHightlightObjectIndex - 1,
				pageResult.getStartIndex(),false);
		if (nextHightlightObjectIndex < 0)
		{
			if (curPage <= 1)
			{
				pageResult.setNeedSwitchPage(false);
				return ERR_NO_CHANGE;
			}
			else
			{
				curPage -= 1;
				pageResult.setCurPage(curPage);
				pageResult.setNeedSwitchPage(true);
				pageResult.setHightLightObjIndex(
						findNextHightlightableObjectIndex(framePtrArray,
								pageResult.getEndIndex(),
								pageResult.getStartIndex(),
								false));
			}
		}
		else
		{
			pageResult.setHightLightObjIndex(nextHightlightObjectIndex);
			pageResult.setNeedSwitchPage(false);
		}
	}
		break;
	case CPageInfo::DOWN:
	{
		nextHightlightObjectIndex = findNextHightlightableObjectIndex(
				framePtrArray, curHightlightObjectIndex + 1,
				pageResult.getEndIndex(),true);
		if (nextHightlightObjectIndex < 0)
		{
			if (curPage >= pageResult.getTotalPage())
			{
				pageResult.setNeedSwitchPage(false);
				return ERR_NO_CHANGE;
			}
			else
			{
				curPage += 1;
				pageResult.setCurPage(curPage);
				pageResult.setNeedSwitchPage(true);
				pageResult.setHightLightObjIndex(
						findNextHightlightableObjectIndex(framePtrArray,
								pageResult.getStartIndex(),
								pageResult.getEndIndex(),true));
			}
		}
		else
		{
			pageResult.setHightLightObjIndex(nextHightlightObjectIndex);
			pageResult.setNeedSwitchPage(false);
		}
	}
		break;
	case CPageInfo::UNKNOW:
	{
		pageResult.setCurPage(curPage);
		pageResult.setNeedSwitchPage(true);
		if (pageResult.isValidHightLightObjIndex())
		{
		pageResult.setHightLightObjIndex(curHightlightObjectIndex);
		}
		else{
			pageResult.setHightLightObjIndex(findNextHightlightableObjectIndex(framePtrArray,
						pageResult.getStartIndex(), pageResult.getEndIndex(),true));
		}
	}
		break;
	default:
		pageResult.setNeedSwitchPage(false);
		return ERR_NO_CHANGE;
		break;
	}

	return ERR_SUCCESS;
}

} /* namespace NS_LCD_AFW */
